var cmd, pwd = ".";
window.onload = function(){
  cmd = document.getElementById('cmd');
}

function update(event){
  if(!cmd) return;
  if(cmd.childNodes.length)
    cmd.removeChild(cmd.childNodes.item(0));
  cmd.appendChild(document.createTextNode(event.target.value));
}

function do_command(){
  if(!cmd)return;
  document.getElementById('terminal').appendChild(document.createTextNode("\n"));
  var input = document.getElementById('input');

  var req = new XMLHttpRequest();
  req.open('POST', location.pathname, false);
  req.send('mode=admin:exec&cmd='+input.value+'&interm=true&pwd='+pwd);
  if(req.status == 200){
    var rv = req.responseText;
    document.getElementById('terminal').appendChild(document.createTextNode(rv));
    pwd = rv.split(":");
    pwd = pwd[pwd.length-1];
    pwd = pwd.substring(0,pwd.length-2);
  }else alert("error");

  input.value='';
  cmd.removeAttribute('id');
  cmd = document.createElement('span');
  cmd.setAttribute('id', 'cmd');
  var terminal = document.getElementById('terminal')
  terminal.appendChild(cmd);
  terminal.scrollTop = terminal.scrollHeight;
}
